'use strict';
angular.module('app')
    .controller('forumPostController', [ '$rootScope', '$scope', '$http', '$state','$timeout',
        function($rootScope, $scope, $http, $state, $timeout) {
    	
            $scope.title = '发帖管理';
            $scope.searchParams = { };
            $scope.loading = false;
            $scope.param = {};
            
            $scope.search = function () {
            	$scope.selected = [];
                $scope.loading = true;
                
                $("#allCheck").attr("checked", false);
                $.ajax({
                    url : $scope.app.project_name + '/forumPost/read/list',
                    data: $scope.searchParams
                }).then(function(result) {
                    console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            $scope.searchRe = function () {
            	$scope.searchParams.startTime = $("#startTime").val();
                $scope.searchParams.endTime = $("#endTime").val();
                
                if ($("#startTime").val() != '' && $("#endTime").val() != '') {
                	if($scope.searchParams.startTime > $scope.searchParams.endTime) {
                        $scope.alertModal({'content': '开始时间不能大于结束时间'});
                        return;
                    }
                }
                
            	$scope.selected = [];
                $scope.loading = true;
                
                $("#allCheck").attr("checked", false);
                $scope.searchParams.pageNum = 1;
                $.ajax({
                    url : $scope.app.project_name + '/forumPost/read/list',
                    data: $scope.searchParams
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }
            
            $scope.getForumChannelList = function (){
                $.ajax({
                    url : $scope.app.project_name + '/myChannel/getForumChannelList'
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.channelList = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
    		}

            $scope.getPostTypelist = function (){
                $.ajax({
                    url : $scope.app.project_name + '/forumPost/read/postTypelist'
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.postTypeList = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
    		}

            $scope.getLabelList = function (){
                $.ajax({
                    url : $scope.app.project_name + '/label/getLabelList'
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.labelList = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
    		}

            $scope.search();
            $scope.getForumChannelList();
             $scope.getPostTypelist();
            $scope.getLabelList();

            $scope.clearSearch = function() {
                $scope.searchParams = { };
                $scope.search();
            }
            
            $scope.selected = [];
    		var updateSelected = function (action, contact) {
    			if (action == 'add' && $scope.selected.indexOf(contact.id) == -1){
    				$scope.selected.push(contact.id);
    				contact.checked = true;
    			}

    			if (action == 'remove' && $scope.selected.indexOf(contact.id) != -1){
    				$scope.selected.splice($scope.selected.indexOf(contact.id), 1);
    				contact.checked = false;
    			}
    		};

    		$scope.updateSelection = function ($event, contact) {
    			var checkbox = $event.target;
    			var action = (checkbox.checked ? 'add' : 'remove');
    			if (action == 'add' && $scope.selected.indexOf(contact.id) == -1){
    				$scope.selected.push(contact.id);
    				contact.checked = true;
    			}
    			if (action == 'remove' && $scope.selected.indexOf(contact.id) != -1){
    				$scope.selected.splice($scope.selected.indexOf(contact.id), 1);
    				contact.checked = false;
    			}
    		};

    		// 全选操作
            $scope.selectAll = function ($event) {
            	var checkbox = $event.target;
            	var action = (checkbox.checked ? 'add' : 'remove');
            	
            	for (var i = 0; i < $scope.pageInfo.records.length; i++) {
            		var contact = $scope.pageInfo.records[i];
            		updateSelected(action, contact);
            	}
            };
            
            $scope.isSelected = function (id) {
            	return $scope.selected.indexOf(id) >= 0;
            };

            // 翻页
            $scope.pagination = function (pageNo) {
                $scope.searchParams.pageNum = pageNo;
                $scope.search();
            };

            // 禁用启用操作
            $scope.enable = function(id, enable) {
            	var ids = [];
            	
                if (id == null || id == '') {
                    ids = $scope.selected;
                } else {
                    ids.push(id);
                }

                var idStr = ids.join(',');
                if (idStr == null || idStr == '') {
                    $scope.alertModal({"content":"请选择需要启用或禁用的记录"});
                    return;
                }
                
                var msg = '';
                // 判断是否有不符合操作的记录
                var count = 0;
                var nocount = 0;
            	if (enable == 'Y') {
            		msg = '启用';
            		
            		angular.forEach($scope.pageInfo.records, function (item) {
                        if (item.enable == 'N' && item.checked && item.status != 6) {
                            count++;
                        } else if (item.checked && (item.enable != 'N' || item.status == 6)) {
                            nocount++;
                        }
                    })
            	}
            	
            	if (enable == 'N') {
            		msg = '停用';
            	}
                
                if (nocount > 0) {
                    $scope.alertModal({"content":msg + count + "条,不符合操作条件的" + nocount + "条"});
                    return;
                }
            	
            	$scope.alertMessage({
                    text:"确定要" + msg + "选中的帖子吗？", //内容
                    btnCancel:true, //取消按钮
                    btnConfirm:true //确认按钮
                }).then(function () {
        			$.ajax({
        				url : $scope.app.project_name + '/forumPost/update/enable',
        				data: {'ids': idStr, 'enable':enable},
        				traditional: true
        			}).then(function(result) {
        		        $scope.loading = false;
        				if (result.httpCode == 200) {
        					$scope.param.keyword = null;
        					$scope.selected = [];
        					$scope.search();
        				} else {
        					$scope.msg = result.msg;
        				}
        			});
                })
            }
            
            // 置顶
            $scope.isTop = function(id, isTop) {
            	var ids = [];
            	
                if (id == null || id == '') {
                    ids = $scope.selected;
                } else {
                    ids.push(id);
                }

                var idStr = ids.join(',');
                if (idStr == null || idStr == '') {
                    $scope.alertModal({"content":"请选择需要置顶的记录"});
                    return;
                }
                
                var msg = '';
                // 判断是否有不符合操作的记录
                var count = 0;
                var nocount = 0;
            	if (isTop == 'Y') {
            		msg = '置顶';
            		
            		angular.forEach($scope.pageInfo.records, function (item) {
                        if (item.isTop == 'N' && item.checked && item.enable == 'Y' && item.status != 6) {
                            count++;
                        } else if (item.checked && (item.isTop != 'N' || item.enable != 'Y' ||　item.status == 6)) {
                            nocount++;
                        }
                    })
            	}
            	
            	if (isTop == 'N') {
            		msg = '取消置顶';
            		
            		angular.forEach($scope.pageInfo.records, function (item) {
                        if (item.isTop == 'Y' && item.checked) {
                            count++;
                        } else if (item.checked && item.isTop != 'Y') {
                            nocount++;
                        }
                    })
            	}
                
                if (nocount > 0) {
                    $scope.alertModal({"content":msg + count + "条,不符合操作条件的" + nocount + "条"});
                    return;
                }
            	
            	$scope.alertMessage({
                    text:"确定要" + msg + "选中的帖子吗？", //内容
                    btnCancel:true, //取消按钮
                    btnConfirm:true //确认按钮
                }).then(function () {
        			$.ajax({
        				url : $scope.app.project_name + '/forumPost/update/isTop',
        				data: {'ids': idStr, 'isTop':isTop},
        				traditional: true
        			}).then(function(result) {
        		        $scope.loading = false;
        				if (result.httpCode == 200) {
        					$scope.param.keyword = null;
        					$scope.selected = [];
        					$scope.search();
        				} else {
        					$scope.msg = result.msg;
        				}
        			});
                })
            }
            
            // 推荐操作
            $scope.isRecommend = function(id, isRecommend) {
            	var ids = [];
            	
                if (id == null || id == '') {
                    ids = $scope.selected;
                } else {
                    ids.push(id);
                }

                var idStr = ids.join(',');
                if (idStr == null || idStr == '') {
                    $scope.alertModal({"content":"请选择需要推荐的记录"});
                    return;
                }
                
                var msg = '';
                // 判断是否有不符合操作的记录
                var count = 0;
                var nocount = 0;
            	if (isRecommend == 'Y') {
            		msg = '推荐';
            		
            		angular.forEach($scope.pageInfo.records, function (item) {
                        if (item.isRecommend == 'N' && item.checked && item.enable == 'Y' && item.status != 6) {
                            count++;
                        } else if (item.checked && (item.isRecommend != 'N' || item.enable != 'Y' ||　item.status == 6)) {
                            nocount++;
                        }
                    })
            	}
            	
            	if (isRecommend == 'N') {
            		msg = '不推荐';
            	}
                
                if (nocount > 0) {
                    $scope.alertModal({"content":msg + count + "条,不符合操作条件的" + nocount + "条"});
                    return;
                }
            	
            	$scope.alertMessage({
                    text:"确定要" + msg + "选中的帖子吗？", //内容
                    btnCancel:true, //取消按钮
                    btnConfirm:true //确认按钮
                }).then(function () {
        			$.ajax({
        				url : $scope.app.project_name + '/forumPost/update/isRecommend',
        				data: {'ids': idStr, 'isRecommend':isRecommend},
        				traditional: true
        			}).then(function(result) {
        		        $scope.loading = false;
        				if (result.httpCode == 200) {
        					$scope.param.keyword = null;
        					$scope.selected = [];
        					$scope.search();
        				} else {
        					$scope.msg = result.msg;
        				}
        			});
                })
            }

            // 鼠标移入的动作
            $scope.introduction = [];
            $scope.preview = function(introduction) {
                var showDiv = document.getElementById('showDiv');
                showDiv.style.left = event.clientX;
                showDiv.style.top = event.clientY;
                showDiv.style.display = 'block';
                showDiv.innerHTML = introduction;
            }
            
            // 鼠标移出的动作
            $scope.outHide = function() {
                var showDiv = document.getElementById('showDiv');
                showDiv.style.display = 'none';
                showDiv.innerHTML = '';
            }

            // 鼠标移入的动作
            $scope.introduction = [];
            $scope.preview1 = function(introduction) {
                var showDiv1 = document.getElementById('showDiv');
                showDiv1.style.left = event.clientX;
                showDiv1.style.top = event.clientY;
                showDiv1.style.display = 'block';
                showDiv1.innerHTML = introduction;
            }
            
            // 鼠标移出的动作
            $scope.outHide1 = function() {
                var showDiv1 = document.getElementById('showDiv');
                showDiv1.style.display = 'none';
                showDiv1.innerHTML = '';
            }
            
            $scope.stopShow = function () {
                $scope.myShow = false;
            };
            
            //预览
            $scope.onPreview = function (data) {
                console.log(data)
                $scope.myShow = true;
                $scope.dataUrl = data.messageVideo;
            };
            
            $scope.getFileExt=function (str) {
                var d=/\.[^\.]+$/.exec(str);
                return d[0].toLowerCase();
            }
            
            // 查询标签表的标签
            $scope.addLabel = function(id){
                $.ajax({
                    url : $scope.app.project_name + '/userCenter/user/labelList',
                    data: {'id': id}
                }).then(function(result) {
                    console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.$apply(function () {
                            $scope.pageInfos = result.data;
                        })
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();

                });
                $scope.id = id;
                $scope.firstApply = true;
            }

            // 查询添加过的帖子标签
            $scope.selectLabel = function(id){
                $.ajax({
                    url : $scope.app.project_name + '/userCenter/user/postLabelAlready',
                    data: {'id': id}
                }).then(function(result) {
                    console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.$apply(function () {
                            $scope.pageInfoAdd = result.data;
                        })
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();

                });
                $scope.id = id;
                $scope.firstApply1 = true;
            }
            
            // 全选操作
            $scope.all = function (m) {
                for(var i = 0;i < $scope.pageInfos.length;i++){
                    if (m == true) {
                        $scope.pageInfos[i].state=true;
                    } else {
                        $scope.pageInfos[i].state=false;
                    }
                }
            };
            
            // 获取复选框选中的值
            $scope.saveMakeUp = function (id) {
                var batchUpdates = new Array();//声明一个数组来保存多个的id值
                $(".checkitem").each(function () {
                    if(this.checked){
                        console.log($(this).val());
                        batchUpdates.push($(this).val());
                    }
                })
                // 给帖子打标签
                $.ajax({
                    url : $scope.app.project_name + '/userCenter/user/addPostlabel',
                    data: {'id':id,'ids': batchUpdates.join(",")}
                }).then(function(result) {
                    console.log(result);
                    if(result.httpCode ==200){
                        $scope.alertModal({"content":"操作成功"});
                        $timeout(function(){
                            $scope.firstApply = false;
                        },2000);
                    }else{
                        $scope.alertModal({"content":"操作失败"});
                        $scope.isDisabled = false;
                    }
                    $scope.loading = false;

                });

            };

        } ]);